const db = require('../dbs/voiceAccountBook');

/*
* 查询语句
* $gt:大于
  $lt:小于
  $gte:大于或等于
  $lte:小于或等于
* */


function dateFormat(dateObj) {
  let year = dateObj.getFullYear();
  let month = dateObj.getMonth() + 1;
  let date = dateObj.getDate();
  return `${year}-${month > 9 ? month : '0' + month}-${date > 9 ? date : '0' + date}`;
}

/*
* 计算指定月多少天
* */
function computedMonthDays(year, month) {
  return new Date(year, month, 0).getDate();
}

/*
* 计算当前周的具体日期
* return [开始, 结束]
* */
function computedWeekDay() {
  const date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  date.setMilliseconds(0);
  const now = date.getDay() === 0 ? 7 : date.getDay();
  const startDate = new Date(date.setDate(date.getDate() - now + 1));
  const endDate = new Date(date.setDate(startDate.getDate() + 6));
  return [
    startDate.getTime(),
    endDate.getTime()
  ];
}

/*
* 计算每周的数据
* */
async function chartWeekData(user) {
  console.log('开始查询', user);
  const weekDay = computedWeekDay();
  const data = await db.aggregate({
    match: {
      user,
      dateTime: {
        $gte: weekDay[0],
        $lte: weekDay[1]
      }
    },
    group: {
      _id: '$date',
      value: {
        $sum: '$money'
      }
    }
  });
  return data;
}

/*
* 计算本月的数据
* */
async function chartMonthData(user) {
  const dateObj = new Date();
  const year = dateObj.getFullYear();
  const month = dateObj.getMonth() + 1;
  const data = await db.aggregate({
    match: {
      user,
      year,
      month,
    },
    group: {
      _id: '$date',
      value: {
        $sum: '$money'
      }
    }
  });
  return data;
}

/*
* 计算本年的数据
* */
async function chartYearData(user) {
  const dateObj = new Date();
  const year = dateObj.getFullYear();
  const data = await db.aggregate({
    match: {
      user,
      year,
    },
    group: {
      _id: '$month',
      value: {
        $sum: '$money'
      }
    }
  });
  return data;
}

/*
* 计算某天不同付款方式的数据
* */
async function chartDateData(user, date, type, typeValue) {
  const data = await db.aggregate({
    match: {
      user,
      date,
      [type]: typeValue
    },
    group: {
      _id: null,
      value: {
        $sum: '$money'
      }
    }
  });
  return data;
}

/*
* 计算某天或某月或某年的数据
* @param user 要查询的用户
* @param type 0 周 1 月 2 年
* @param date, year, month 日, 月, 年
* */
async function chartYearMonthDayData(user, type, {date, year, month}) {
  let match;
  switch (type) {
    case '0':
      const weekDay = computedWeekDay();
      match = {
        user,
        dateTime: {
          $gte: weekDay[0],
          $lte: weekDay[1]
        }
      };
      break;
    case '1':
      match = {
        user,
        month,
        year
      };
      break;
    case '2':
      match = {
        user,
        year
      };
      break;
    default:
      break;
  }
  const data = await db.aggregate({
    match,
    group: {
      _id: null,
      value: {
        $sum: '$money'
      }
    }
  });
  console.log(data);
  if (data[0]) {
    return data[0].value;
  } else {
    return 0;
  }
}

/*
* 获取环形图的数据
* @param user 用户
* @param type '0' 日 date必传
* @param type '1' 月 month, year必传
* @param dataType payType  付款类型,  consumptionType 消费类型
* */
async function pieData(user, type, dataType, {date, month, year}) {
  let match;
  switch (type) {
    case '0':
      match = {
        user,
        date
      };
      break;
    case '1':
      match = {
        user,
        month,
        year
      };
      break;
  }
  const data = await db.aggregate({
    match,
    group: {
      _id: '$' + dataType,
      value: {
        $sum: '$money'
      }
    }
  });
  return data
}

module.exports = {
  chartYearMonthDayData,
  chartWeekData,
  chartMonthData,
  chartYearData,
  pieData
};
